<template>
  <div class="app">
    <a-breadcrumb style="margin-bottom: 10px">
      <a-breadcrumb-item>岗位管理</a-breadcrumb-item>
    </a-breadcrumb>
    <a-row class="pd20 mt20">
      <a-col :span="6">
        岗位名：
        <a-input placeholder="请输入" v-model="queryForm.postName" />
      </a-col>
      <a-col :span="4">
        <a-button type="primary" @click="getList"> 查询 </a-button>&emsp;&nbsp;
        <a-button @click="cleanQuery">重置</a-button>
      </a-col>
    </a-row>
    <a-row class="pd20 mt20">
      <div class="btn_box">
        <div>岗位列表</div>
        <a-button type="primary" @click="addFn" v-hasPermi="['post:add']"
          >添加</a-button
        >
      </div>
      <a-spin :spinning="loading">
        <a-table
          :scroll="{ x: 500 }"
          :columns="columns"
          :data-source="tableData"
          :pagination="false"
        >
          <a slot="name" slot-scope="text">{{ text }}</a>
          <span slot="customTitle"><a-icon type="smile-o" /> Name</span>
          <span slot="action" slot-scope="text, record">
            <a @click="roleFn(text, record)" v-hasPermi="['post:role']"
              >关联角色</a
            >
            <a-divider type="vertical" />
            <a @click="editFn(text, record)" v-hasPermi="['post:edit']">修改</a>
            <a-divider type="vertical" />
            <a
              @click="delFn(text, record)"
              class="red"
              v-hasPermi="['post:del']"
              >删除</a
            >
          </span>
        </a-table>
        <div class="page_box">
          <a-pagination
            v-model="queryForm.offset"
            :page-size-options="['20', '30', '40', '50']"
            :total="total"
            show-size-changer
            :page-size="queryForm.current"
            @showSizeChange="onShowSizeChange"
            @change="pageChange"
            :show-total="(total) => `共 ${total} 条`"
          ></a-pagination>
        </div>
      </a-spin>
    </a-row>
    <a-modal
      title="岗位配置"
      :visible="dialogVisible"
      width="38%"
      ok-text="确认"
      cancel-text="取消"
      @cancel="dialogVisible = false"
      @ok="PutPost"
    >
      <a-form :form="form" v-if="type == 'Create'">
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="岗位名称"
        >
          <a-input
            v-decorator="[
              'postName',
              { rules: [{ required: true, message: '请输入岗位名称' }] },
            ]"
            placeholder="请输入岗位名称"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="选择部门"
        >
          <a-tree-select
            show-search
            style="width: 100%"
            v-decorator="[
              'deptId',
              { rules: [{ required: true, message: '请选择部门' }] },
            ]"
            @change="changeDept"
            :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
            placeholder="请选择部门"
            allow-clear
            tree-default-expand-all
            :treeData="deptList"
            :replaceFields="{
              title: 'deptName',
              children: 'childList',
              value: 'id',
            }"
          >
          </a-tree-select>
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="是否部门负责人"
        >
          <a-radio-group
            v-decorator="[
              'isDeptBoss',
              { rules: [{ required: true, message: '请是否部门负责人' }] },
            ]"
          >
            <a-radio :value="'1'"> 是 </a-radio>
            <a-radio :value="'0'"> 否 </a-radio></a-radio-group
          >
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="排序"
        >
          <a-input-number
            v-decorator="[
              'postSort',
              { rules: [{ required: true, message: '请输入排序' }] },
            ]"
            placeholder="请输入排序"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="备注"
        >
          <a-input v-decorator="['remark']" placeholder="请输入备注" />
        </a-form-item>
      </a-form>
      <div v-if="type == 'role'">
        <a-spin :spinning="loading">
          <div>关联角色</div>
          <a-form-item
            :label-col="formItemLayout.labelCol"
            :wrapper-col="formItemLayout.wrapperCol"
            label="角色名称"
          >
            <a-select
              style="width: 120px"
              v-model="roleId"
              placeholder="请选择角色"
            >
              <a-select-option
                v-for="(item, index) in roleList"
                :key="index"
                :value="item.roleId"
              >
                {{ item.roleName }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-spin>
      </div>
    </a-modal>
  </div>
</template>

<script>
import { Modal } from "ant-design-vue";
import { message } from "ant-design-vue";
export default {
  data() {
    return {
      loading: true,
      dialogVisible: false,
      queryForm: { current: 20, offset: 1 },
      total: 0,
      form: this.$form.createForm(this, { name: "coordinated" }),
      id: null,
      postId: null,
      deptId: null,
      roleId: null,
      type: "Create",
      roleList: [],
      tableData: [],
      columns: [
        {
          title: "岗位名称",
          dataIndex: "postName",
          key: "postName",
        },
        {
          title: "顺序",
          dataIndex: "postSort",
          key: "postSort",
        },
        {
          title: "备注",
          dataIndex: "remark",
          key: "remark",
        },
        {
          title: "操作",
          key: "action",
          scopedSlots: { customRender: "action" },
        },
      ],
      formItemLayout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 19 },
      },
      formTailLayout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 19, offset: 4 },
      },
      deptList: [],
    };
  },
  mounted() {
    this.getList();
    this.getDeptList();
    this.getRoleList();
  },
  methods: {
    getList() {
      this.loading = true;
      const url = "/government-platform-center/post";
      const params = this.queryForm;
      this.$axios
        .get(url, {
          params: params,
        })
        .then((res) => {
          if (res.data.code == 0) {
            this.tableData = res.data.data;
            this.total = res.data.total;
            this.loading = false;
          }
        });
    },
    cleanQuery() {
      this.queryForm = {};
      this.getList();
    },
    // 分页
    onShowSizeChange(current, pageSize) {
      this.queryForm.current = pageSize;
      this.getList();
    },
    pageChange(current) {
      this.queryForm.offset = current;
      this.getList();
    },
    delFn(text, record) {
      console.log(text, record);
      const that = this;
      Modal.confirm({
        title: "确认删除",
        content: "您确定要删除这项内容吗？",
        okText: "确认",
        cancelText: "取消",
        onOk() {
          const url = `/government-platform-center/post/deleteById?id=${text.postId}`;
          that.$axios.post(url).then((res) => {
            if (res.data.code == 0) {
              that.getList();
              message.success("操作成功");
            }
          });
        },
        onCancel() {
          // 在此处理取消逻辑
          console.log("删除已取消");
        },
      });
    },
    addFn() {
      this.dialogVisible = true;
      this.id = null;
      this.form.resetFields();
    },
    editFn(text, record) {
      this.dialogVisible = true;
      const url = `/government-platform-center/post/${text.postId}`;
      const params = this.queryForm;
      this.$axios
        .get(url, {
          params: params,
        })
        .then((res) => {
          if (res.data.code == 0) {
            this.form.setFieldsValue(res.data.data);
            this.id = res.data.data.dictId;
          }
        });
    },
    PutPost() {
      this.form.validateFields((err, values) => {
        if (err) {
          return;
        }
        if (this.type == "Create") {
          if (!this.id) {
            const url = "/government-platform-center/post";
            const form = { ...values };
            form.deptId = values.deptId;
            this.$axios
              .post(url, form, {
                headers: {
                  "Content-Type": "application/x-www-form-urlencoded",
                },
              })
              .then((res) => {
                if (res.data.code == 0) {
                  this.getList();
                  this.reSetForm();
                  this.dialogVisible = false;
                  message.success("操作成功");
                }
              });
          } else {
            const form = { ...values, dictId: this.id };
            const url = "/government-platform-center/post/edit";
            this.$axios
              .post(url, form, {
                headers: {
                  "Content-Type": "application/x-www-form-urlencoded",
                },
              })
              .then((res) => {
                if (res.data.code == 0) {
                  this.getList();
                  this.reSetForm();
                  this.dialogVisible = false;
                  message.success("操作成功");
                }
              });
          }
        } else if (this.type == "role") {
          const url = "/government-platform-center/roleDept/edit";
          const form = {
            deptId: this.deptId,
            postId: this.postId,
            roleId: this.roleId,
          };
          console.log(form, "form");
          this.$axios
            .post(url, form, {
              headers: {
                "Content-Type": "application/x-www-form-urlencoded",
              },
            })
            .then((res) => {
              if (res.data.code == 0) {
                this.getList();
                this.dialogVisible = false;
                message.success("操作成功");
              }
            });
        }
      });
    },
    reSetForm() {
      // this.form = { dictName: null, dictType: null, remark: null }
    },
    // 关联部门模块
    changeDept(e) {
      console.log(e, this.deptId);
    },
    // 部门列表
    getDeptList() {
      const url = "/government-platform-center/dept";
      const params = this.queryForm;
      this.$axios
        .get(url, {
          params: params,
        })
        .then((res) => {
          if (res.data.code == 0) {
            this.deptList = res.data.data;
            this.getOther(this.deptList);
          }
        });
    },
    getOther(list) {
      list.forEach((item, index) => {
        item = { ...item, ...item.thisData };
        // item['']
        this.$set(list, index, item);
        if (item.childList && item.childList.length > 0) {
          this.getOther(item.childList);
        }
      });
    },
    //关联
    roleFn(text, record) {
      this.loading = true;
      this.type = "role";
      this.dialogVisible = true;
      this.roleId = null;
      this.postId = text.postId;
      this.deptId = text.deptId;

      const url = `/government-platform-center/roleDept/queryDeptByRole?postId=${text.postId}`;
      this.$axios.get(url).then((res) => {
        if (res.data.code == 0 && res.data.data != null) {
          this.roleId = res.data.data.roleId;
        }
        this.loading = false;
      });
    },
    getRoleList() {
      this.loading = true;
      const url = "/government-platform-center/role";
      const params = { current: 999, offset: 1 };
      this.$axios
        .get(url, {
          params: params,
        })
        .then((res) => {
          if (res.data.code == 0) {
            this.roleList = res.data.data;
          }
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.ant-row {
  border-radius: 5px;

  background: #fff;
  color: #000;
}

v-deep {
  .ant-table-thead > tr > th {
    color: #000;
    font-weight: bold;
  }
  .ant-table-tbody > tr > td {
    color: #000;
  }
  .el-table thead {
    color: black;
    font-weight: bold;
    background: #fafafa;
  }
  .el-table th.el-table__cell {
    background: #fafafa;
  }
}

.ant-col {
  margin: 10px 0;
}

.ant-select {
  width: 60%;
}

.ant-input {
  width: 60%;
}

@media screen and (max-width: 1300px) {
  .el-input {
    width: 70%;
  }

  .el-select {
    width: 70%;
  }

  .app {
    width: calc(100% - 50px);
  }
}

@media screen and (max-width: 1100px) {
  .el-input {
    width: 90%;
  }

  .el-select {
    width: 90%;
  }
}
</style>
